package com.example.demo.controller;


import com.example.demo.annotation.AuthCheck;
import com.example.demo.common.BaseResponse;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.ResultUtils;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ThrowUtils;
import com.example.demo.myModel.entity.Role;
import com.example.demo.myModel.otd.user.*;
import com.example.demo.myModel.entity.Email;
import com.example.demo.myModel.entity.MyUser;
import com.example.demo.myModel.vo.UserLoginVo;
import com.example.demo.service.My.MyUserService;
import com.example.demo.service.My.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/myUser")
@Slf4j
public class MyUserController {

    @Resource
    private MyUserService myUserService;

    /**
     * 发送注册码给邮箱
     *
     * @param sentEmailRequest 请求参数{
     *                         "userId":12553315621
     *                         "email":2222@qq.com
     *                         }
     * @return {
     * "code":200,
     * "message":null,
     * "data":265656
     * }
     */
    @PostMapping("/sent/Email")
    public BaseResponse<Boolean> sentEmail(@RequestBody SentEmailRequest sentEmailRequest, HttpSession session) {
        Email email = new Email();
        email.setEmail(sentEmailRequest.getEmail());
        email.setUserId(sentEmailRequest.getUserId());
        boolean b =myUserService.sentEmailCode(email, session);
        return ResultUtils.success(b);
    }


    /**
     * 注册
     *
     * @param registerRequest 注册请求参数
     * @return {
     * "id":***************
     * }
     */
    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody RegisterRequest registerRequest, HttpSession session) {
        MyUser myUser = new MyUser();
        myUser.setUserName(registerRequest.getUserName());
        myUser.setUserId(registerRequest.getUserId());
        myUser.setPassword(registerRequest.getPassword());
        myUser.setEmail(registerRequest.getEmail());
        myUser.setRoleId(registerRequest.getRoleId());
        myUser.setCode(registerRequest.getCode());
        myUser.setCollege(registerRequest.getCollege());
        myUser.setSchoolName(registerRequest.getSchoolName());
        myUser.setTel(registerRequest.getTel());
        myUser.setCreateTime(LocalDateTime.now());
        myUser.setGender(registerRequest.getGender());
        myUser.setStatus(1);
        long id = myUserService.userRegister(myUser, session);
        return ResultUtils.success(id);
    }


    /**
     * 学生登录功能
     *
     * @param userLoginRequest 登录信息{"userAccount":*******,
     *                         "password":******}
     * @param request          请求相关信息
     * @return {
     * "code":200,
     * *     "message":null,
     * *     "data":true
     * }
     */
    @PostMapping("/login")
    public BaseResponse<Boolean> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        myUserService.userLogin(userLoginRequest, request);
        return ResultUtils.success(true);
    }


    /**
     * 获取当前登录用户
     *
     * @param request 请求内容
     * @return {
     * userLoginVo
     * }
     */
    @GetMapping("/get/UserLogin")
    public BaseResponse<UserLoginVo> getUserLogin(HttpServletRequest request) {

        UserLoginVo userLoginVo = new UserLoginVo();
        MyUser user = myUserService.getLoginUser(request);
        return ResultUtils.success(myUserService.getLoginUserVO(user));
    }

}